package com.xzgj.fmmall.controller;

import com.github.pagehelper.PageInfo;
import com.xzgj.fmmall.entity.Goods;
import com.xzgj.fmmall.service.ProductCommentsService;
import com.xzgj.fmmall.service.ProductService;
import com.xzgj.fmmall.vo.ProductCommentsVO;
import com.xzgj.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
@Api(value = "提供商品添加、修改、删除和查询的相关接口",tags = "商品管理")

public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private ProductCommentsService productCommentsService;

    @ApiOperation("提供商品评价统计查询接口")
    @ApiImplicitParam(dataType = "string", name = "productId", value = "商品id", required = true, defaultValue = "1")
    @GetMapping(value = "/commentsCount/{productId}")
    public ResultVO getProductCommentsCountByProductId(@PathVariable("productId") String productId) {
        return productCommentsService.getCommentsCountByProductId(productId);
    }

    @ApiOperation("提供商品评论信息查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "productId", value = "商品id", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "pageNo", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "页码size", required = true, defaultValue = "10")
    })
    @GetMapping(value = "/comments/{productId}")
    public PageInfo<ProductCommentsVO> selectProductCommentsByProductId(@PathVariable("productId") String productId, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return productCommentsService.selectProductCommentsByProductId(productId, pageNo, pageSize);
    }

    @ApiOperation("提供商品参数信息查询接口")
    @ApiImplicitParam(dataType = "string", name = "pid", value = "商品id", required = true, defaultValue = "1")
    @GetMapping(value = "/detail/params/{pid}")
    public ResultVO getProductsParamsInfo(@PathVariable("pid") String pid) {
        return productService.getProductsParamsByProductId(pid);
    }

    @ApiOperation("提供商品信息相关的接口")
    @ApiImplicitParam(dataType = "string", name = "pid", value = "商品id", required = true, defaultValue = "1")
    @GetMapping(value = "/detail/info/{pid}")
    public ResultVO getProductsBasicInfo(@PathVariable("pid") String pid) {
        return productService.getProductsBasicInfo(pid);
    }

    //只有一个参数时，默认为get
    @ApiOperation("查询推荐的商品")
    @ApiImplicitParam(dataType = "int", name = "productId", value = "商品id", required = true, defaultValue = "1")
    @GetMapping(value = "/recommend/list")
    public ResultVO listProducts(int productId) {
        return productService.selectRecommendProducts(productId);
    }

    @ApiOperation("查询十款最新推荐的商品")
    @ApiImplicitParam(dataType = "int", name = "productId", value = "商品id", required = true, defaultValue = "1")
    @GetMapping(value = "/latest/recommend/list")
    public ResultVO listLatestProducts() {
        return productService.selectLatestRecommendProducts();
    }

    @ApiOperation("查询每个分类下销量前6的商品")
    @ApiImplicitParam(dataType = "int", name = "rootCategoryId", value = "分类id", required = true, defaultValue = "1")
    @GetMapping(value = "/rootCategoryId/list")
    public ResultVO listTopSoldNumProducts(int rootCategoryId) {
        return productService.selectTopProductsOnSoldNumEveryCategory(rootCategoryId);
    }

    @ApiOperation("提供类别查询商品接口")
    @GetMapping(value = "/listByCategoryId/{categoryId}")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "categoryId", value = "类别id", required = true),
            @ApiImplicitParam(dataType = "int", name = "pageNo", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "页码size", required = true, defaultValue = "10")
    })
    public ResultVO selectProductsByCategoryId(@PathVariable("categoryId") int categoryId, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return productService.getProductsByCategoryId(categoryId, pageNo, pageSize);
    }

    @ApiOperation("提供类别查询商品品牌接口")
    @GetMapping(value = "/listBrands/{categoryId}")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "categoryId", value = "类别id", required = true)
    })
    public ResultVO selectBrandsByCategoryId(@PathVariable("categoryId") int categoryId) {
        return productService.listBrands(categoryId);
    }


    @ApiOperation("提供关键字查询商品接口")
    @GetMapping(value = "/listByKeyword")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "keyword", value = "搜索关键字", required = true),
            @ApiImplicitParam(dataType = "int", name = "pageNo", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "页码size", required = true, defaultValue = "10")
    })
    public ResultVO searchProductsByKeyword(String keyword, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return productService.searchProducts(keyword, pageNo, pageSize);
    }

    @ApiOperation("提供关键字查询商品品牌接口")
    @GetMapping(value = "/listBrandsByKeyword")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "keyword", value = "搜索关键字", required = true)
    })
    public ResultVO selectBrandsByKeyword(String keyword) {
        return productService.listBrands(keyword);
    }

    @ApiOperation("根据商品id查询当前商品所有套餐中价格最低的套餐")
    @GetMapping(value = "/listLowestPriceByProductId")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "productId", value = "商品id", required = true)
    })
    public ResultVO selectLowestPriceByProductId(String productId) {
        return productService.selectLowestPriceByProductId(productId);
    }
}